package vn.com.epi.algorithm.clustering;

import java.util.ArrayList;
import java.util.List;

public class Data {

	private static int n;
	protected static float[] total;
	public static List<Point>[] users;
	
	public Data() {
		
	}
	
	@SuppressWarnings("unchecked")
	public Data(int n_) {
		n = n_;
		total = new float[n];
		users = new List[n];
		for (int i = 0; i < n; i++)
			users[i] = new ArrayList<Point>();
	}
	
	public static float distance(int u, int v) {
		int i, j;
		float sum;
		i = 0;
		j = 0;
		sum = 0;		
		while (i < Data.users[u].size() && j < Data.users[v].size()) {
			if (Data.users[u].get(i).getIndex() == Data.users[v].get(j).getIndex()) {
				sum += Math.min(Data.users[u].get(i).getValue(), Data.users[v].get(j).getValue());
				i++;
				j++;
			} else if (Data.users[u].get(i).getIndex() < Data.users[v].get(j).getIndex()) 
				i++;
			else 
				j++;
		}
		return sum / (Data.total[u] + Data.total[v] - sum);
	}
	
	public static void initial() {
		for (int i = 0; i < n; i++) {
			total[i] = 0;
			for (int j = 0; j < users[i].size(); j++)
				total[i] += users[i].get(j).getValue();
		}			
	}	
	
	public static void add(int userId, short index, short value) {
		users[userId].add(new Point(index, value));
	}
	
	public static void remove(int v) {
		users[v] = null;
	}
	
}
